# Create your views here.
from datetime import datetime

from pprint import pprint
import string
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.views.decorators.csrf import csrf_protect
from exams.models import Exam, ExamQuestions, ExamAnswers, UserAnsweredExam


def home(request):
#  pprint (vars(request.user))
#  pprint(vars(request.user.ldap_user))
    exams = Exam.objects.all()

    return render_to_response("index.html",{'exams':exams})


def login(request):
    return render_to_response("login.html")


def adminHome(request):
    exams = Exam.objects.all()
    obj = {"exams": exams}
    return render_to_response('adminHome.html', obj)


@csrf_protect
def addExam(request):
    if request.method == 'GET':
        return render_to_response('new_exam.html',context_instance=RequestContext(request))
    else:
        form_data = request.POST
        saveName = form_data["name"]
        saveDateCreated = form_data["date_begin"]
        saveDateEnd = form_data["date_end"]
        saveDetails = form_data["details"]
        new_exam = Exam(name=saveName, date_created=saveDateCreated, details=saveDetails, date_end=saveDateEnd)
        new_exam.save()
        for i in range(1, 10):
            numberI = str(i)
            form_question = form_data["question_" + numberI]
            new_question = ExamQuestions(idOfExam=new_exam.id, question=form_question)
            correct = form_data["question_" + numberI + "_" +numberI + "_" + numberI]
            new_question.save()
            for x in range(1, 5):
                numberX = str(x)
                form_answer = form_data["question_" + numberI + "_" + numberX]
                if correct == numberX:
                    check = True
                else:
                    check = False
                new_answer = ExamAnswers(idOfQuestion=new_question.id, answer=form_answer, isCorrect=check)
                new_answer.save()



        return HttpResponseRedirect('/exams/adminHome/')

@csrf_protect
def getExamById(request,id):
    user = "tryggvim10@ru.is"
    howManyCorrectCounter = 0
    exam = Exam.objects.get(id = id)
    questions = ExamQuestions.objects.filter(idOfExam = id)
    question_list = list(questions)

    for question in question_list:
        new_answer =  ExamAnswers.objects.filter(idOfQuestion = question.id)
        question.answers = new_answer


    if request.method == 'GET':
        return render_to_response('examByID.html',{'exam':exam,'questions':questions},context_instance=RequestContext(request))
    else:
        userAnswer = UserAnsweredExam(name=user,idOfExam = exam.id,howManyCorrect = howManyCorrectCounter)
        form_data = request.POST
        for question in question_list:
            correct = form_data["Question_"+str(question.id)]
            correctAnswers= list(question.answers)
            for answers in correctAnswers:
                if correct == answers.answer:
                    if answers.isCorrect is True:
                        userAnswer.howManyCorrect =+ 1



        userAnswer.save()
        return HttpResponseRedirect('/exams/home/')
